package com.shop.autoshop.pay.wxpay;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lyt
 * @version 1.0.0
 * @ClassName WxPayConfiguration.java
 * @Description TODO
 * @createTime 2022年07月27日 11:59:00
 */
@Configuration
@ConditionalOnClass(WxPayService.class)
@EnableConfigurationProperties(WxPayProperty.class)
public class WxPayConfiguration {

    @Resource
    private WxPayProperty wxPayProperty;

    private static Map<String, WxPayService> wxPayServices = new HashMap<>();

    /**
     * @Description 默认公众号appid
     * @author: ybwei
     * @Date: 2021/3/22 15:17
     */
    private String defaultAppid = null;


    @PostConstruct
    public void init() {
        //1、赋值wxPayServices
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(StringUtils.trimToNull(wxPayProperty.getAppId()));
        payConfig.setMchId(StringUtils.trimToNull(wxPayProperty.getMchId()));
        payConfig.setMchKey(StringUtils.trimToNull(wxPayProperty.getKey()));
        payConfig.setKeyPath(StringUtils.trimToNull(wxPayProperty.getCertPath()));
        // 可以指定是否使用沙箱环境
        payConfig.setUseSandboxEnv(false);

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        wxPayServices.put(wxPayProperty.getAppId(), wxPayService);
    }

    /**
     * @Description 根据appid获取WxPayService
     * @Author ybwei
     * @Date 2021/3/22 15:01
     * @Param [appid]
     * @Return com.github.binarywang.wxpay.service.WxPayService
     * @Exception
     */
    public WxPayService getWxPayService() {
        WxPayService wxService = wxPayServices.get(wxPayProperty.getAppId());
        if (wxService == null) {
            throw new IllegalArgumentException(String.format("未找到对应appid=[%s]的配置，请核实！", wxPayProperty.getAppId()));
        }
        return wxService;
    }


    public String getRemoteHost(javax.servlet.http.HttpServletRequest request){
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getHeader("X-Real-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)){
            ip = request.getRemoteAddr();
        }
        ip = ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
        return ip.split(",")[0];
    }

    private static String[] parsePatterns = {"yyyyMMddHHmmss"};

    public Date wxNotifyDateFormate(String str) {
        try {
            return DateUtils.parseDate(str, parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

}
